/*
 * Package il.ac.biu.cs.grossmm.test.SimpleProtocol
 * File SubscribeRequest.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.foo.protocol;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SubscribeRequest extends Request {

	static int counter = new Random().nextInt();;

	static Lock lock = new ReentrantLock();

	static String uid = "s" + Long.toHexString(System.currentTimeMillis());


	String sid;

	String watcher;

	String resource;

	String resourceType;

	String format;

	String filter;

	String filterType;

	int duration;

	public SubscribeRequest(String watcher, String resource, int duration)
	{
		this(watcher, resource, "presence", "foo.PresenceDoc", duration, null, null);
	}

	public String getResourceType()
	{
		return resourceType;
	}

	public SubscribeRequest(String watcher, String resource,
		String resourceType, String format, int duration, String filterType,
		String filter)
	{
		lock.lock();
		sid = uid + Integer.toHexString(counter++);
		lock.unlock();

		this.watcher = watcher;

		this.resource = resource;
		
		this.resourceType = resourceType;
		
		this.format = format;

		this.duration = duration;

		this.filter = filter;
		this.filterType = filterType;
	}

	public SubscribeRequest(String tid, String[] params)
	{
		super(tid);

		sid = params[0];
		watcher = params[1];
		resource = params[2];
		resourceType = params[3];
		format = params[4];
		duration = Integer.parseInt(params[5]);
		
		if( params.length == 8 ) {
			filterType = params[6];
			filter = params[7];
		}
	}

	@Override
	public String getCommand()
	{
		return "SUBSCRIBE";
	}

	@Override
	public String[] getParameters()
	{
		if( filterType == null )
		return new String[] { sid, watcher, resource, resourceType, format,
			Integer.toString(duration)};
		else
			return new String[] { sid, watcher, resource, resourceType, format,
			Integer.toString(duration), filterType == null ? "" : filterType,
			filter == null ? "" : filter };
	}

	public static void understand()
	{
		RequestFactory fac = new RequestFactory() {
			public Request getRequest(String tid, String[] params)
			{
				return new SubscribeRequest(tid, params);
			}
		};

		factories.put("SUBSCRIBE", fac);
	}

	public int getDuration()
	{
		return duration;
	}

	public String getResource()
	{
		return resource;
	}

	public String getSid()
	{
		return sid;
	}

	public String getWatcher()
	{
		return watcher;
	}

	public String getFilter()
	{
		return filter;
	}

	public String getFilterType()
	{
		return filterType;
	}

	public String getFormat()
	{
		return format;
	}
}
